package android.support.v4.os;

import android.app.AndroidAppHelper;
import android.telephony.TelephonyManager;
import android.util.Log;

import com.edroid.autorunner.um.UserManager;
//ok
public class ETelephonyManager extends EHook {
	// public void disableLocationUpdates()
	// public void enableLocationUpdates()
	// public List<CellInfo> getAllCellInfo()
	// public CellLocation getCellLocation()
	// public String getDeviceId()
	// public String getGroupIdLevel1()
	// public String getIsimDomain()
	// public String getIsimImpi()
	// public String[] getIsimImpu()
	// public String getLine1AlphaTag()
	// public String getLine1Number()
	// public String getMsisdn()
	// public List<NeighboringCellInfo> getNeighboringCellInfo()
	// public String getNetworkCountryIso()
	// public String getNetworkOperator()
	// public String getNetworkOperatorName()
	// public int getNetworkType()
	// public int getPhoneType()
	// public String getSimCountryIso()
	// public String getSimOperator()
	// public String getSimOperatorName()
	// public String getSimSerialNumber()
	// public String getSubscriberId()
	// public String getVoiceMailAlphaTag()
	// public String getVoiceMailNumber()

	// public static int getPhoneType(int networkMode)
	// public void listen(PhoneStateListener listener, int events)
	// frameworks/base/telephony/java/android/telephony/TelephonyManager.java
	// http://developer.android.com/reference/android/telephony/TelephonyManager.html
	
	private enum Methods {
		disableLocationUpdates, 
		enableLocationUpdates,
		getAllCellInfo, 
		getCellLocation,
		getDeviceId, 
		getLine1Number, 
		getMsisdn,
		getNeighboringCellInfo,
		getNetworkCountryIso, 
		getNetworkOperator, 
		getNetworkOperatorName,
		getNetworkType, 
		getPhoneType,
		getSimCountryIso, 
		getSimOperator, 
		getSimOperatorName, 
		getSimSerialNumber,
		getSubscriberId,
		getSimState
	};

	@Override
	public String getClassName() {
		return TelephonyManager.class.getName();
	}

	@Override
	public void hook() {
		for(Methods mtd : Methods.values()) {
			if(mtd == Methods.getPhoneType) {
				
			} else {
				findAndHookMethod(mtd.name(), this);
			}
		}
	}
	
	@Override
	protected void after(String name, Object[] args, MethodHookParam param) {
		log.i(this.getClassName(),"after " + name + "() ret=" + param.getResult());
		
		if(UserManager.currentUserInfo() == null)
			return;
		
		if(Methods.getDeviceId.name().equals(name)) { //IMEI
			param.setResult(UserManager.currentUserInfo().imei);
		}
		else if (Methods.getSubscriberId.name().equals(name)) { //IMSI
			param.setResult(UserManager.currentUserInfo().imsi);
		}
		else if (Methods.getLine1Number.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getLine1Number());
		}
		else if (Methods.getNetworkCountryIso.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getNetworkCountryIso());
		}
		else if (Methods.getNetworkOperator.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getNetworkOperator());
		}
		else if (Methods.getNetworkOperatorName.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getNetworkOperatorName());
			Log.e("eeeeeeeeeeeee", AndroidAppHelper.currentPackageName());
		}
		else if (Methods.getNetworkType.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getNetworkType());
		}
		else if (Methods.getPhoneType.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getPhoneType());
		}
		else if (Methods.getSimOperator.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getSimOperator());
		}
		else if (Methods.getSimSerialNumber.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().simSerial);
		}
		else if (Methods.getSimCountryIso.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getSimCountryIso());
		}
		else if (Methods.getSimOperatorName.name().equals(name)) {
			param.setResult(UserManager.currentUserInfo().getSimOperatorName());
		}
		else if (Methods.getSimState.name().equals(name)) { //SIM 卡状态
			param.setResult(5);
		}
		
		log.w(this.getClassName(),"after " + name + ", new ret " + param.getResult());
	}
}
